<?php
class ContractorsAction extends CommonAction {
    
    public function index() {
    	import('ORG.Util.Page');
    	$filter_list = array("name","country","isverify","hasCase");
    	$f = array();
    	$query = array();
    	foreach($filter_list as $vo) {
    		$f[$vo] = $this->_request($vo);
    	}
    	$param = "";
    	if ($f["name"]) { 
    		$query["username"] = array("like","%".$f["name"]."%");
    		$param .= "name=".$f["name"]."&";
    	}
    	if ($f["country"]) {
    		$query["country"] = M("country")->getFieldByEn($f["country"],"code");
    		$param .= "country=".$f["country"]."&";
    	}
    	if ($f["isverify"]) {
    		$query["isverify"] = 4;
    		$param .= "isverify=1&";
    	}
    	if ($f["hasCase"]) {
    		$query["democount"] = array("gt",0);
    		$param .= "hasCase=1&";
    	}
    	$_count = M("User")->where($query)->count();
    	$_page = new Page($_count,15); //
    	$_page->url = U('/contractor/index')."?".$param."p=__PAGE__";
    	$_list = M("User")->where($query)
    			->limit($_page->firstRow.','.$_page->listRows)
    			->select();
    	$this->assign("_page",$_page->show());
    	$this->assign("_list",$_list);
    	$this->display("contractor");
    }
    
    public function view() {
    	$uid = intval($this->_get("uid"));
    	$_profile = M("user")->where(array("id"=>$uid))->find();
    	$_skill   = M("usermap")->join("__CGSKILL__ on __USERMAP__.vid = __CGSKILL__.id")->where(array("type"=>1,"userid"=>$uid))->select();
    	
    	$this->assign("profile",$_profile);
    	$this->assign("skill",$_skill);
    	$this->display("contractor-single");
    }
    public function works() {
    	$uid = intval($this->_get("uid"));
    	$_profile = M("user")->where(array("id"=>$uid))->find();
    	$_project = M("project")->where(array("userid"=>$uid))->select();
    	$_bidding = M("probid")->where(array("uid"=>$uid))
    			->join("__PROJECT__ on __PROJECT__.id = __PROBID__.pid")
    			->select();
    	
    	$this->assign("profile",$_profile);
    	$this->assign("project",$_project);
    	$this->assign("bidding",$_bidding);
    	$this->display("contractor-single-work");
    }
    
    public function caseshow() {
    	$uid = intval($this->_get("uid"));
    	$_profile = M("user")->where(array("id"=>$uid))->find();
    	$_list = M("userdemo")->where(array("userid"=>$uid))->select();
    	
    	$this->assign("profile",$_profile);
    	$this->assign("list",$_list);
    	$this->display("contractor-single-cases");
    }
    
    public function follow() {
    	$action = $this->_get("action");
    	if ($this->_user) {
    		$uid = intval($this->_get("uid"));
    		if ($action == "cancel") {
    			M("userfollow")->where(array("userid"=>$this->_user["id"],"followuid"=>$uid))->delete();
    		} else {
	    		M("userfollow")->data(array("userid"=>$this->_user["id"],"followuid"=>$uid))->add();
	    	}
	    	echo json_encode(array("error"=>0,"data"=>followStatus($this->_user,$uid)));
    	} else {
    		echo json_encode(array("error"=>1,"msg"=>L('请先登录')));
    	}
    }
    
    protected function _initialize(){
		parent::_initialize();
		$this->assign("nav_menu","contractor");
	}
}